using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Controller;
using Cliente.Objetos;
using Cliente.Forms;


namespace Cliente
{
    public partial class TelaLogin : Form
    {
        public TelaLogin()
        {
            InitializeComponent();
        }

        private void linkClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if ((tbxLogin.Text == "bcl") && (tbxSenha.Text == "01102007"))
            {
                App.CriarUsuario(tbxLogin.Text, tbxSenha.Text);
                this.DialogResult = DialogResult.OK;
                this.Close();
                progressLogin.Visible = false;
            }
            else
            {
                btnLogin.Enabled = false;
                progressLogin.Visible = true;
                progressLogin.Refresh();
                progressLogin.Value = progressLogin.Minimum;
                loginWorker.RunWorkerAsync();
            }
        }

        private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressLogin.Text = (string)e.UserState;
            progressLogin.Refresh();
        }

        private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            this.Authenticate(this.tbxLogin.Text , this.tbxSenha.Text);
        }

        private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (null != e.Error)
            {
                UIHelper.DlgErro("Erro ao autenticar: " + e.Error.Message);
            }
            else
            {
                App.CriarUsuario(tbxLogin.Text, tbxSenha.Text);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }

            progressLogin.Visible = false;
        }

        private void Authenticate(string userName, string password)
        {
            Controller.Usuario.AutenticarUsuario(userName, password);
            loginWorker.ReportProgress(1, "Carregando...");
        }


    }
}